#include<bits/stdc++.h>
#include<cassert>

using namespace std;

pair<int, int> f(vector<int> d, vector<int> p){
    pair<int, int> ans = {0, 0};
    vector<pair<int, int>> h;

    for(int i = 0; i < p.size(); ++i)
        h.push_back({p[i], d[i]});

    sort(h.begin(), h.end(), [](pair<int, int> a, pair<int, int> b)
            { return a.first > b.first; });

    vector<bool> s(p.size());

    for(int i = 0; i < p.size(); i++) {
        int b = (p.size() < h[i].second ? p.size() : h[i].second) - 1;
        for(int j = b; j >= 0; j--)
            if(s[j] == 0){
                s[j] = 1;
                ans.first++;
                ans.second += h[i].first;
                break;
            }
    }

    return ans;
}
int main() {
    vector<int> d = {4, 1, 1, 1};
    vector<int> p = {20, 10, 40, 30};
    pair<int, int> r1= {2, 60};
    pair<int, int> r2= {2, 127};
    assert(f(d, p) == r1);
    assert(f({2,1,2,1,1}, {100,19,27, 25, 15}) == r2);
    return 0;
}
